package o;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.channels.FileChannel.MapMode;
import java.util.concurrent.atomic.AtomicReference;

public final class aam {
    private static final AtomicReference<byte[]> ˎ = new AtomicReference();

    static final class c {
        final byte[] ˊ;
        final int ˏ;
        final int ॱ;

        c(@NonNull byte[] bArr, int i, int i2) {
            this.ˊ = bArr;
            this.ॱ = i;
            this.ˏ = i2;
        }
    }

    static class e extends InputStream {
        private int ˏ = -1;
        @NonNull
        private final ByteBuffer ॱ;

        e(@NonNull ByteBuffer byteBuffer) {
            this.ॱ = byteBuffer;
        }

        public int available() {
            return this.ॱ.remaining();
        }

        public int read() {
            if (this.ॱ.hasRemaining()) {
                return this.ॱ.get() & 255;
            }
            return -1;
        }

        public synchronized void mark(int i) {
            this.ˏ = this.ॱ.position();
        }

        public boolean markSupported() {
            return true;
        }

        public int read(@NonNull byte[] bArr, int i, int i2) throws IOException {
            if (!this.ॱ.hasRemaining()) {
                return -1;
            }
            int min = Math.min(i2, available());
            this.ॱ.get(bArr, i, min);
            return min;
        }

        public synchronized void reset() throws IOException {
            if (this.ˏ == -1) {
                throw new IOException("Cannot reset to unset mark position");
            }
            this.ॱ.position(this.ˏ);
        }

        public long skip(long j) throws IOException {
            if (!this.ॱ.hasRemaining()) {
                return -1;
            }
            long min = Math.min(j, (long) available());
            this.ॱ.position((int) (((long) this.ॱ.position()) + min));
            return min;
        }
    }

    @NonNull
    public static ByteBuffer ˏ(@NonNull File file) throws IOException {
        RandomAccessFile randomAccessFile;
        Throwable th;
        FileChannel fileChannel = null;
        try {
            long length = file.length();
            if (length > 2147483647L) {
                throw new IOException("File too large to map into memory");
            } else if (length == 0) {
                throw new IOException("File unsuitable for memory mapping");
            } else {
                RandomAccessFile randomAccessFile2 = new RandomAccessFile(file, "r");
                try {
                    FileChannel channel = randomAccessFile2.getChannel();
                    try {
                        ByteBuffer load = channel.map(MapMode.READ_ONLY, 0, length).load();
                        if (channel != null) {
                            try {
                                channel.close();
                            } catch (IOException e) {
                            }
                        }
                        if (randomAccessFile2 != null) {
                            try {
                                randomAccessFile2.close();
                            } catch (IOException e2) {
                            }
                        }
                        return load;
                    } catch (Throwable th2) {
                        randomAccessFile = randomAccessFile2;
                        FileChannel fileChannel2 = channel;
                        th = th2;
                        fileChannel = fileChannel2;
                        if (fileChannel != null) {
                            try {
                                fileChannel.close();
                            } catch (IOException e3) {
                            }
                        }
                        if (randomAccessFile != null) {
                            try {
                                randomAccessFile.close();
                            } catch (IOException e4) {
                            }
                        }
                        throw th;
                    }
                } catch (Throwable th3) {
                    th = th3;
                    randomAccessFile = randomAccessFile2;
                    if (fileChannel != null) {
                        fileChannel.close();
                    }
                    if (randomAccessFile != null) {
                        randomAccessFile.close();
                    }
                    throw th;
                }
            }
        } catch (Throwable th4) {
            th = th4;
            randomAccessFile = null;
            if (fileChannel != null) {
                fileChannel.close();
            }
            if (randomAccessFile != null) {
                randomAccessFile.close();
            }
            throw th;
        }
    }

    public static void ˊ(@NonNull ByteBuffer byteBuffer, @NonNull File file) throws IOException {
        RandomAccessFile randomAccessFile;
        Throwable th;
        FileChannel fileChannel = null;
        byteBuffer.position(0);
        try {
            randomAccessFile = new RandomAccessFile(file, "rw");
            try {
                fileChannel = randomAccessFile.getChannel();
                fileChannel.write(byteBuffer);
                fileChannel.force(false);
                fileChannel.close();
                randomAccessFile.close();
                if (fileChannel != null) {
                    try {
                        fileChannel.close();
                    } catch (IOException e) {
                    }
                }
                if (randomAccessFile != null) {
                    try {
                        randomAccessFile.close();
                    } catch (IOException e2) {
                    }
                }
            } catch (Throwable th2) {
                th = th2;
                if (fileChannel != null) {
                    try {
                        fileChannel.close();
                    } catch (IOException e3) {
                    }
                }
                if (randomAccessFile != null) {
                    try {
                        randomAccessFile.close();
                    } catch (IOException e4) {
                    }
                }
                throw th;
            }
        } catch (Throwable th3) {
            th = th3;
            randomAccessFile = null;
            if (fileChannel != null) {
                fileChannel.close();
            }
            if (randomAccessFile != null) {
                randomAccessFile.close();
            }
            throw th;
        }
    }

    @NonNull
    public static byte[] ˋ(@NonNull ByteBuffer byteBuffer) {
        c ॱ = ॱ(byteBuffer);
        if (ॱ != null && ॱ.ॱ == 0 && ॱ.ˏ == ॱ.ˊ.length) {
            return byteBuffer.array();
        }
        ByteBuffer asReadOnlyBuffer = byteBuffer.asReadOnlyBuffer();
        byte[] bArr = new byte[asReadOnlyBuffer.limit()];
        asReadOnlyBuffer.position(0);
        asReadOnlyBuffer.get(bArr);
        return bArr;
    }

    @NonNull
    public static InputStream ˊ(@NonNull ByteBuffer byteBuffer) {
        return new e(byteBuffer);
    }

    @NonNull
    public static ByteBuffer ˋ(@NonNull InputStream inputStream) throws IOException {
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(16384);
        Object obj = (byte[]) ˎ.getAndSet(null);
        if (obj == null) {
            obj = new byte[16384];
        }
        while (true) {
            int read = inputStream.read(obj);
            if (read >= 0) {
                byteArrayOutputStream.write(obj, 0, read);
            } else {
                ˎ.set(obj);
                byte[] toByteArray = byteArrayOutputStream.toByteArray();
                return (ByteBuffer) ByteBuffer.allocateDirect(toByteArray.length).put(toByteArray).position(0);
            }
        }
    }

    @Nullable
    private static c ॱ(@NonNull ByteBuffer byteBuffer) {
        if (byteBuffer.isReadOnly() || !byteBuffer.hasArray()) {
            return null;
        }
        return new c(byteBuffer.array(), byteBuffer.arrayOffset(), byteBuffer.limit());
    }
}
